package com.example.httpmode;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private SharedPreferences config;
    private EditText login_ed_user;
    private EditText login_ed_pwd;
    private Button login_but_login;
    private ImageView login_iv_setting;
    private AlertDialog show;
    private ImageView main_iv_setting;
    private ImageView main_iv_logoff;
    private ImageView main_iv_room;
    private ImageView main_iv_kitchen;
    private ImageView main_iv_bedroom1;
    private ImageView main_iv_bedroom2;
    private ImageView main_iv_toilet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //查找控件

        main_iv_setting = findViewById(R.id.main_iv_setting);
        main_iv_logoff = findViewById(R.id.main_iv_logoff);
        main_iv_room = findViewById(R.id.main_iv_room);
        main_iv_kitchen = findViewById(R.id.main_iv_kitchen);
        main_iv_bedroom1 = findViewById(R.id.main_iv_bedroom1);
        main_iv_bedroom2 = findViewById(R.id.main_iv_bedroom2);
        main_iv_toilet = findViewById(R.id.main_iv_toilet);

        //在config中获取token
        config = getSharedPreferences("config", MODE_PRIVATE);
        String token = config.getString("token", "");

        if(config != null && !"".equals(config) && token != null && !"".equals(token)){
            //main代码
        }else{
            //login
            login();
        }

        main_iv_setting.setOnClickListener(this);
        main_iv_logoff.setOnClickListener(this);
        main_iv_room.setOnClickListener(this);
        main_iv_kitchen.setOnClickListener(this);
        main_iv_bedroom1.setOnClickListener(this);
        main_iv_bedroom2.setOnClickListener(this);
        main_iv_toilet.setOnClickListener(this);
    }

    private void login() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this,0);
        View inflate = LayoutInflater.from(this).inflate(R.layout.login, null);
        builder.setView(inflate);
        builder.setCancelable(false);
        login_ed_user = inflate.findViewById(R.id.login_ed_user);
        login_ed_pwd = inflate.findViewById(R.id.login_ed_pwd);
        login_but_login = inflate.findViewById(R.id.login_but_login);
        login_iv_setting = inflate.findViewById(R.id.login_iv_setting);
        login_but_login.setOnClickListener(this);
        login_iv_setting.setOnClickListener(this);
        show = builder.show();
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.login_but_login:
                //子线程网络请求
                String ip = config.getString("ip", "");
                String port = config.getString("port", "");
                String user = login_ed_user.getText().toString();
                String pwd = login_ed_pwd.getText().toString();
                new Thread(){
                    @Override
                    public void run() {
                        String login_url = "http://"+ip+"/Users/Login";
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("Account",user);
                            jsonObject.put("Password",pwd);
                            jsonObject.put("IsRememberMe",true);
                            String pamer = jsonObject.toString();
                            String login_json = HttpUrlConnetc.post(login_url, pamer, null);
                            //System.out.println(login_json);
                            JSONObject jsonObject1 = new JSONObject(login_json);
                            JSONObject resultObj = jsonObject1.getJSONObject("ResultObj");
                            String accessToken = resultObj.getString("AccessToken");
                            //System.out.println(accessToken);
                            SharedPreferences.Editor edit = config.edit();
                            edit.putString("token",accessToken);
                            edit.commit();
                            //弹框取消
                            show.dismiss();
                            //Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.login_iv_setting:
            case R.id.main_iv_setting:
                Intent intent = new Intent(MainActivity.this,SettingActivity2.class);
                startActivity(intent);
                overridePendingTransition(0,0);
                break;
            case R.id.main_iv_logoff:
                SharedPreferences.Editor edit = config.edit();
                edit.remove("token");
                edit.commit();
                login();
                break;
            case R.id.main_iv_room:
                //界面跳转
                Intent intent1 = new Intent(MainActivity.this,RoomActivity.class);
                startActivity(intent1);
                overridePendingTransition(0,0);
                break;
            case R.id.main_iv_kitchen:
                //界面跳转
                Intent intent2 = new Intent(MainActivity.this, KitchenActivity.class);
                startActivity(intent2);
                overridePendingTransition(0,0);
                break;
            case R.id.main_iv_bedroom1:
                //界面跳转
                Intent intent3 = new Intent(MainActivity.this, Bedroom1Activity.class);
                startActivity(intent3);
                overridePendingTransition(0,0);
                break;
            case R.id.main_iv_bedroom2:
                //界面跳转
                Intent intent4 = new Intent(MainActivity.this, Bedroom2Activity.class);
                startActivity(intent4);
                overridePendingTransition(0,0);
                break;
            case R.id.main_iv_toilet:
                //界面跳转
                Intent intent5 = new Intent(MainActivity.this, ToiletActivity.class);
                startActivity(intent5);
                overridePendingTransition(0,0);
                break;
        }
    }
}